// 链接MySQL8.x需要使用mysql2；
// https://blog.csdn.net/Alices_zhu/article/details/124754662
const mysql = require("mysql2");

// 创建连接
const connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "xxxx",
  database: "test",
});

// 连接到数据库
connection.connect((err) => {
  if (err) {
    console.error("Error connecting to MySQL database:", err);
    return;
  }
  console.log("Connected to MySQL database");

  // 在这里执行你的数据库操作

  connection.query("select * from log", (err, results, fields) => {
    if (err) {
      console.error("Error connecting to MySQL database:", err);
      return;
    }
    console.log("results", results);
    console.log("fields", fields);
  });

  // 断开与数据库的连接
  connection.end((err) => {
    if (err) {
      console.error("Error disconnecting from MySQL database:", err);
      return;
    }
    console.log("Disconnected from MySQL database");
  });
});
